package com.live.shop.home.setting;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.live.shop.BaseActivity;
import com.live.shop.R;
import com.live.shop.sql.UserDatabase;
import com.live.shop.tool.GetUtil;
import com.live.shop.tool.OkHttpUtils;
import com.live.shop.tool.StatusBarUtils;
import com.live.shop.view.ToastBottomUtils;
import com.live.shop.view.ToastUtils;

import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Response;

public class OpinionActivity extends BaseActivity {
    ImageButton opinion_back;
    EditText opinion_input;
    TextView opinion_input_number;
    TextView opinion_button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_opinion);
        StatusBarUtils.setDarkStatusWhite(this,R.color.white);

        opinion_back = findViewById(R.id.opinion_back);
        opinion_input = findViewById(R.id.opinion_input);
        opinion_input_number = findViewById(R.id.opinion_input_number);
        opinion_button = findViewById(R.id.opinion_button);
        opinion_input.addTextChangedListener(mTextWatcher);

        opinion_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        opinion_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    if(opinion_input.getText().toString().isEmpty()){
                        ToastBottomUtils.Setinfo(getApplicationContext(),"请填写内容！");
                    }else{
                        UserDatabase user = new UserDatabase(getApplicationContext());
                        JSONObject json = new JSONObject();
                        json.put("opinion_info",opinion_input.getText().toString());
                        getAccount(GetUtil.submitOpinion(),json.toString(),user.select().getToken());
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        });
    }

    TextWatcher mTextWatcher = new TextWatcher() {
        private String temp;
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,int after) {
        }
        @Override
        public void onTextChanged(CharSequence s, int start, int before,int count) {
            temp = s.toString();
            opinion_input_number.setText(Integer.toString(temp.length()));
        }
        @Override
        public void afterTextChanged(Editable s) {
            temp = s.toString();
            if (!TextUtils.isEmpty(temp)) {
                String limitSubstring = GetUtil.getLimitSubString300(temp);	//limitSubstring是UTF-8编码计算后的字符串
                if (!TextUtils.isEmpty(limitSubstring)) {
                    if (!limitSubstring.equals(temp)) {
                        opinion_input.setText(limitSubstring);
                        opinion_input.setSelection(limitSubstring.length());
                    }
                }
            }
        }
    };

    public static final int RRQUEST_FAILURE = 1;
    public static final int RRQUEST_SUCCESS = 2;

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case RRQUEST_FAILURE:
                    ToastBottomUtils.Setinfo(getApplicationContext(),msg.obj.toString());
                    break;
                case RRQUEST_SUCCESS:
                    try {
                        JSONObject json = new JSONObject(msg.obj.toString());
                        if(json.getInt("code") == 1){
                            ToastUtils.Setinfo(getApplicationContext(),json.getString("info"),1);
                            Thread.sleep(1000);
                            finish();
                        }else{
                            ToastBottomUtils.Setinfo(getApplicationContext(),json.getString("info"));
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    break;
            }
        }
    };
    public void getAccount(String url,String jsonStr,String token){
        OkHttpUtils.sendOkHttpJsonTokenRequest(url, jsonStr,new okhttp3.Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                Message message = new Message();
                message.what = RRQUEST_FAILURE;
                message.obj = "【请求异常】请联系管理员";
                handler.sendMessage(message);
            }
            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                String responseData = response.body().string();
                Message message = new Message();
                message.what = RRQUEST_SUCCESS;
                message.obj = responseData;
                handler.sendMessage(message);
            }
        },token);
    }
}